package com.zhangjm.service;

import com.zhangjm.dao.CitysDao;
import com.zhangjm.pojo.Citys;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class CitysService {

    @Autowired
    private CitysDao citysDao;

    /**
     * 查询所有的菜单
     * @return
     */
    public List<Citys> getAllMenu(){

        //根据角色获取用户的权限信息（获取一级菜单）
        List<Citys> citysList = citysDao.getAllCitysOfFirst();
        //获取一级菜单
        for (Citys citys : citysList) {
            //递归调用获取二级目录信息
            this.getChildrenByPid(citys,citys.getId());
        }
        return citysList;

    }


    /**
     * 获取子菜单的递归方法
     */
    public void getChildrenByPid(Citys citys,Integer pid){

        List<Citys> childList = citysDao.getChildrenListByPid(pid);

        if(childList.size()>0){
            citys.setCitysList(childList);
            for (Citys citys1 : childList) {
                //递归调用获取下级信息
                this.getChildrenByPid(citys1,citys1.getId());
            }
        }else{
            return;
        }
    }
}
